(let (( in (open "c:/---/workspace/Euler/probl18.txt" :if-does-not-exist nil)))
  (when in 
    (loop for line = (read-line in nil)
       while line do (format t "~a~%" line))
    (close in)))

(defun read-line-as-list (line)
  (with-input-from-string (s (concatenate 'string "(" line ")"))
			  (read s)))

(defun load-from-file (filename)
  (let ((n))
    (with-open-file (stream filename) 
      (loop for line = (read-line stream nil) 
	 while line do 
	   ;(append n line) finally (return n)))))
	   ;(format t "~a~%" line) finally (return line)))))
	   (if (null n)
	       (setq n (list (read-line-as-list line)))
	       (setq n (append n (list (read-line-as-list line)))))
	 finally (return n)))))

(print (load-from-file "c:/---/workspace/Euler/probl18.txt"))

(defun print-lists (x)
  (cond ((atom (car x)) (print x) (print-lists (cdr x)))
	(t (or (print-lists (car x))
	       (print-lists (cdr x))))))

(print (routeMax (load-from-file "c:/---/workspace/Euler/probl18.txt") 0 0 ))

(defun elem (n i j)
  (format t "getting element ~d (~d, ~d) ~%" (nth j (nth i n)) i j)
  (nth j (nth i n)))

(defun routeMax(n i j)
  (if (< (length n) (+ i 3)) ;(print "first") (print "good")))
      (+ (elem n i j) (max (elem n (1+ i) j) (elem n (1+ i) (1+ j))))
      (+ (elem n i j) (max (routeMax n (1+ i) j) (routeMax n (1+ i) (1+ j))))))